import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { Button, Spin, Form, Input, Table, DatePicker, Modal, message } from 'antd'
import moment from 'moment';
/* 引用接口文档路径 */
import {fetchHouseCheckList,setBlackListBack,deleteUserSelect} from 'actions/userConter'
import '../../style/userControl.less'

const { MonthPicker, RangePicker } = DatePicker;
const FormItem = Form.Item
@Form.create({})
//abc
@connect(
  (state, props) => ({
    config: state.config,
    userInfoList: state.userInfoList,
  })
)
export default class app extends Component {
  constructor(props) {
    super(props)
    this.state = {
      list:"",//基础数据列表
      createTime:"",
      lastLoginTime:""
    }
    this.keyword={"realName":"","mobile":"","createTime":"","lastLoginTime":"","status":"0"};//搜索关键词
    this.currentNumber = 1;//当前页面
    this.pagesize = 10;//每页条数
  }
  componentDidMount() {
    this.selectList();
  }
  
  selectFn(){//点击搜索按钮
    this.keyword = {
      "realName":this.props.form.getFieldValue('realName'),
      "mobile":this.props.form.getFieldValue('mobile'),
      "createTime":this.state.createTime,
      "lastLoginTime":this.state.lastLoginTime,
      "status":"0"
    };
    this.currentNumber=1;
    this.selectList();
  }
  selectList(pageNumber){//查询列表
    const datajson = Object.assign({"start":pageNumber||1,"end":this.pagesize}, this.keyword);
    this.currentNumber = pageNumber||1;
    this.props.dispatch(fetchHouseCheckList(
      datajson,
      (respose) => {
        this.setState({
          list: respose,
        })
    }))
  }
  
  createTimeFn(value){
    if(value){
      value = value._d.format("yyyy-MM-dd hh:mm:ss") 
    }else{
      value = ""
    }
    this.state.createTime = value 
  }

  lastLoginTimeFn(value){
    if(value){
      value = value._d.format("yyyy-MM-dd hh:mm:ss") 
    }else{
      value = ""
    }
    this.state.lastLoginTime = value 
  }




  outBlackList(id,status){//释放黑名单
    Modal.confirm({
      title: '确定要释放该用户么？',
      onOk:()=>{
        this.props.dispatch(setBlackListBack({"accountId":id,"status":1},(respose) => {
          var length = this.state.list.respData.count ? this.state.list.respData.count : 0;
          if(Math.ceil(length/this.pagesize) == this.currentNumber && length%this.pagesize == 1 && this.currentNumber != 1){
            this.currentNumber = this.currentNumber - 1;
          }
          message.success(respose.respMsg)
          this.selectList(this.currentNumber)
        }))
      },
      onCancel:()=>{

      },
    })
  }

  deleteUserData(id){
    Modal.confirm({
      title: '确定要彻底删除该用户么？',
      onOk:()=>{
        this.props.dispatch(deleteUserSelect({"accountId":id},(respose) => {
          var length = this.state.list.respData.count ? this.state.list.respData.count : 0;
          if(Math.ceil(length/this.pagesize) == this.currentNumber && length%this.pagesize == 1 && this.currentNumber != 1){
            this.currentNumber = this.currentNumber - 1;
          }
          message.success(respose.respMsg)
          this.selectList(this.currentNumber)
        }))
      },
      onCancel:()=>{
      },
    })
  }

  columns() {//表格标题
    return [
      {
        title: '用户编码',
        dataIndex: 'userCode',
        key: 'userCode',
        width: '10%',
      },
      {
        title: '头像',
        dataIndex: 'headImage',
        key: 'headImage',
        width: '10%',
        render: (text, recordId, index) => <img style={{width:'50px'}} src={text ? text : require('../../images/baseHeader.png')} />,
        
      },
      {
        title: '姓名',
        dataIndex: 'realName',
        key: 'realName',
        width: '10%',
      },
      {
        title: '手机号',
        dataIndex: 'mobile',
        key: 'mobile',
        width: '10%',
      },
      {
        title: '公司职位',
        dataIndex: 'positionName',
        key: 'positionName',
        width: '10%',
      },
      {
        title: '注册时间',
        dataIndex: 'createTime',
        key: 'createTime',
        width: '15%',
      },
      {
        title: '最后登录时间',
        dataIndex: 'lastLoginTime',
        key: 'lastLoginTime',
        width: '15%',
      },
      {
        title: '操作',
        width: '10%',
        render: (text, recordId, index) => 
          <div>
            <Button type="primary" size="small" onClick={this.outBlackList.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>释放</Button>
            <Button type="primary" size="small" onClick={this.deleteUserData.bind(this,text.accountId)} style={{marginRight:"5px",marginBottom:"5px"}}>删除</Button>          
          </div>,
        
      },
    ]
  }
  


  render () {
    const {form } = this.props
    const { getFieldDecorator } = form    
    const accountList = this.state.list.respData ? this.state.list.respData : [];
    const dateFormat = 'YYYY/MM/DD';
    return (
      <div className="page">
        {/* 搜索条件筛选块 */}
        <div className="searcDiv">
          <Form onSubmit={this.selectFn.bind(this)} layout="inline">
            <FormItem label="姓名">      
              {
                getFieldDecorator('realName', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }             
            </FormItem>
            <FormItem label="手机号">
              {
                getFieldDecorator('mobile', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }  
            </FormItem>
            <FormItem label="注册时间">
              <DatePicker onChange={this.createTimeFn.bind(this)} format={dateFormat} />
            </FormItem>
            <FormItem label="最后登录时间">
              <DatePicker onChange={this.lastLoginTimeFn.bind(this)} format={dateFormat} />
            </FormItem>
            <Button type="primary" onClick={this.selectFn.bind(this)}>搜索</Button>
          </Form>
        </div>
        {/*数据渲染列表*/}
        <Spin spinning={false}>
          <Table
            rowKey="id" 
            columns={this.columns()}//表格标题列
            dataSource={accountList.accountList}
            pagination={{showTotal:total => `共 ${total} 条数据`, total: accountList.count, current:this.currentNumber,pageSize: this.pagesize , onChange: this.selectList.bind(this)}}
            scroll={{ y: true }}
          />
        </Spin>       
      </div>
    )
  }
}
